package com.wiiss.common.filter;

import com.wiiss.common.constant.Constants;
import com.wiiss.common.utils.uuid.IdUtils;
import org.slf4j.MDC;

import javax.servlet.*;
import java.io.IOException;

/**
 * 在最外层为每条请求增加traceId，以便从头到尾的追踪这条请求的日志
 */
public class MDCFilter implements Filter {


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            MDC.put(Constants.MDC_TRACE_ID, IdUtils.fastSimpleUUID());
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            MDC.clear();
        }
    }
}
